package com.hippy.erya.modular.erya;

import cn.stylefeng.roses.kernel.model.response.ResponseData;
import com.hippy.erya.entity.*;
import com.hippy.erya.impl.HomePageServiceImpl;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.List;
import java.util.StringJoiner;

@Slf4j
@Controller
@RequestMapping("/erya/home")
public class HomePageController {

    @Autowired
    private HomePageServiceImpl homePageService;

    @RequestMapping(value = "/home")
    public String member() {
        return "/erya/home/home_setting.html";
    }

    @RequestMapping("/showAll")
    @ResponseBody
    public ResponseData readMessage() {
        HomePageEntity homePageEntity = homePageService.showHomeDashboard();
        return ResponseData.success(convert(homePageEntity));
    }

    private HomePageRespDTO convert(HomePageEntity homePageEntity) {
        if (homePageEntity == null) {
            return null;
        }
        HomePageRespDTO homePageRespDTO = new HomePageRespDTO();
        homePageRespDTO.setUnReadMessage(convertMessage(homePageEntity.getUnMessage()));
        homePageRespDTO.setCostingEntities(convertCosting(homePageEntity.getCostingEntities()));
        homePageRespDTO.setOrderEvaluateEntities(convertOrder(homePageEntity.getOrderEvaluateEntities()));
        homePageRespDTO.setHotDish(convertHotDish(homePageEntity.getHotDishs()));
        homePageRespDTO.setRawMaterialLoss(convertRawMaterialLoss(homePageEntity.getRawMaterialLossList()));
        return homePageRespDTO;
    }

    private String convertRawMaterialLoss(List<RawMaterialDailyLossEntity> rawMaterialLossList) {
        if (CollectionUtils.isEmpty(rawMaterialLossList)) {
            return null;
        }
        StringJoiner joiner = new StringJoiner("<br>");
        rawMaterialLossList.forEach(loss -> {
            joiner.add("食材名称: <font color=\"red\">" +  loss.getName() + "</font>, 食材消耗: <font color=\"red\">" + loss.getQuantity() + "</font>(斤)");
        });
        return joiner.toString();
    }

    private String convertHotDish(List<HotDishEntity> hotDishs) {
        if (CollectionUtils.isEmpty(hotDishs)) {
            return null;
        }
        StringJoiner joiner = new StringJoiner("<br>");
        hotDishs.forEach(hotDish -> {
            joiner.add("菜品名称: <font color=\"red\">" +  hotDish.getName() + "</font>, 销量: <font color=\"red\">" + hotDish.getCount() + "</font>");
        });
        return joiner.toString();
    }


    private String convertOrder(List<OrderEvaluateEntity> orderEvaluateEntities) {
        if (CollectionUtils.isEmpty(orderEvaluateEntities)) {
            return null;
        }
        StringJoiner joiner = new StringJoiner("<br>");
        orderEvaluateEntities.forEach(order -> {
            if (order.getBadScoreCount() > 0) {
                joiner.add(order.getDate() + "，差评: <font color=\"red\">" + order.getBadScoreCount() + "</font>");
                if (CollectionUtils.isEmpty(order.getBadOrderContents())) {
                    return;
                }
                order.getBadOrderContents().forEach(orderContent -> {
                    joiner.add("差评内容: " + orderContent);
                });
            }
        });
        return joiner.toString();
    }

    private String convertCosting(List<LatestCostingEntity> costingEntities) {
        if (CollectionUtils.isEmpty(costingEntities)) {
            return null;
        }
        StringJoiner joiner = new StringJoiner("<br>");
        costingEntities.forEach(costing -> {
            BigDecimal income = BigDecimal.valueOf(costing.getOrderAllIncome());
            income = income.setScale(2, BigDecimal.ROUND_HALF_UP);

            BigDecimal cost = BigDecimal.valueOf(costing.getAllCost());
            cost = cost.setScale(2, BigDecimal.ROUND_HALF_UP);

            BigDecimal profit = BigDecimal.valueOf(costing.getAllProfit());
            profit = profit.setScale(2, BigDecimal.ROUND_HALF_UP);

            joiner.add(costing.getDate() + " 收入: <font color=\"red\">" + income.toPlainString() + "</font>元，支出: <font color=\"green\">" + cost.toPlainString() + "</font>元，净赚: " + profit.toPlainString() + "元");
        });
        return joiner.toString();
    }

    private String convertMessage(UnMessageEntity unMessage) {
        if (unMessage == null) {
            return null;
        }
        StringJoiner joiner = new StringJoiner("<br>");
        List<String> contents = unMessage.getContents();
        if (!CollectionUtils.isEmpty(contents)) {
            contents.forEach(joiner::add);
        }
        joiner.add("未读消息数: <font color=\"red\">" + unMessage.getCount() + "</font>");
        return joiner.toString();
    }

    @Data
    private static class HomePageRespDTO {
        private String unReadMessage;
        private String hotDish;
        private String rawMaterialLoss;
        private String costingEntities;
        private String orderEvaluateEntities;
    }

}
